package com.abredatos.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.util.ArrayList;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.jsoup.nodes.Element;


import com.abredatos.entities.ObrasPlanE;

public class Parser {

	private final static String uri = "https://ssweb.mpt.es/infofondolocal/info_ciudadanos/ajax_process.php?load=carga_tabla_detalle_ciudadanos&id_municipio=3535";

	public Parser() {
		
	}
	
	public static ArrayList<ObrasPlanE> parsear() {
		 HttpClient client = new DefaultHttpClient();
     	 HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit
     	 
     	 /*
     	  * Establecimiento de la cabecera POST
     	  */
     	 HttpGet get = new HttpGet(uri);
     	 HttpResponse response = null;
     	 InputStream in = null;
     	 try {
			response = client.execute(get);
			in = response.getEntity().getContent();
			String texto = convertStreamToString(in);
			texto = texto.substring(texto.indexOf("<td nowrap="));
			texto.charAt(1);
			
			Document doc = Jsoup.connect(uri)
	     	  .data("query", "Java")
	     	  .userAgent("Mozilla")
	     	  .cookie("auth", "token")
	     	  .timeout(3000)
	     	  .post();
			
			Element link = doc.select("a").first();
		//	Elements info = doc.select("tr").f
			String text = doc.body().text(); // "An example link"
			String linkHref = link.attr("href"); // "http://example.com/"
			String linkText = link.text(); // "example""

			String linkOuterH = link.outerHtml(); 
			    // "<a href="http://example.com"><b>example</b></a>"
			String linkInnerH = link.html(); // "<b>example</b>"
			
     	 } catch (ClientProtocolException e) {
			e.printStackTrace();
     	 } catch (IOException e) {
			e.printStackTrace();
     	 }
     	
		return null;
		
	}
	 public static String convertStreamToString(InputStream is)
     throws IOException {
		 /*
		  * To convert the InputStream to String we use the
		  * Reader.read(char[] buffer) method. We iterate until the
		  * Reader return -1 which means there's no more data to
		  * read. We use the StringWriter class to produce the string.
		  */
		 if (is != null) {
		     Writer writer = new StringWriter();
		
		     char[] buffer = new char[1024];
		     try {
		         Reader reader = new BufferedReader(  new InputStreamReader(is, "UTF-8"));
		         int n;
		         while ((n = reader.read(buffer)) != -1) {
		             writer.write(buffer, 0, n);
		         }
		     } finally {
		         is.close();
		     }
		     return writer.toString();
		 } else {        
		     return "";
		 }
} 
}
